﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufValueInput.h"
#include "GnufSliderControl.h"

namespace gnuf {
    struct SliderProperty{
        int value;
        int minimun;
        int maximun;
        std::wstring unit;
        SliderProperty(const int v,
            const int mi,
            const int ma,
            const std::wstring& u) :
            value(v),
            minimun(mi),
            maximun(ma),
            unit(u) {};
    } ;
    class GNUF_BASE_EXPORT SliderValueInput 
        : public ValueInput<int>
        , public ISliderControlHandler
    {
        DEFINE_CAST_DERIVED(gnuf::SliderValueInput, gnuf::ValueInputBase)
    public:
        explicit SliderValueInput(std::uint16_t, const SliderProperty&, bool readonly, gcmp::OwnerPtr< Info > &);
        virtual ~SliderValueInput();

        inline const SliderControl * sliderControl() const { return m_sliderCtrl.get(); }
        inline       SliderControl * sliderControl()       { return m_sliderCtrl.get(); }
        inline const std::pair<int, int>& range() { return m_range; }
    protected:
        // Inherits from ValueInput...
        virtual void onPostValueChanged() override;
        virtual void onPostAbilityChanged() override;

        // Inherits from ISliderControlHandler...
        virtual void onValueChanged(const int) override;
        virtual void onRangeChanged(const std::pair<int, int> &) override;

        void onPostRangeChanged();
        void onPreRangeChanged(const std::pair<int,int> &) {};
        
        void setRangeNoiselessly(const std::pair<int, int>& sr) { setRange(sr, true); }
    private:
        inline void setRange(const std::pair<int, int>& sr, const bool noiselessly)
        {
            if (sr == m_range)
            {
                return;
            }
            if (!noiselessly)
            {
                onPreRangeChanged(sr);
            }
            m_range = sr;
            if (!noiselessly)
            {
                onPostRangeChanged();
            }
        }
        std::pair<int, int> m_range;
        gcmp::OwnerPtr< SliderControl > m_sliderCtrl;
    };

}
